package com.easyctrl.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.easyctrl.iface.BaseDBImpl;
import com.easyctrl.ldy.domain.DeviceBean;

public class DeviceManager extends DataBaseManage implements
		BaseDBImpl<DeviceBean> {

	private static DeviceManager instance;

	private DeviceManager(Context context) {
		super(context);
	}

	public static synchronized DeviceManager getInstance(Context context) {
		if (instance == null) {
			instance = new DeviceManager(context);
		}
		return instance;
	}

	public void deleteAll() {
		synchronized (LOCK) {
			openWriteDB();
			database.delete(DeviceBean.Table.T_NAME, null, null);
			database.execSQL(" DELETE FROM sqlite_sequence ");
		}
	}

	@Override
	public void add(DeviceBean t) {
		openWriteDB();
		String sql = " insert into " + DeviceBean.Table.T_NAME
				+ " (device_name,device_type,device_isSystem) values(?,?,?) ";
		try {
			database.execSQL(sql, new Object[] { t.device_name, t.device_type,
					t.device_isSystem });
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int delete(int id) {
		openWriteDB();
		int colunms = 0;
		try {
			colunms = database.delete(DeviceBean.Table.T_NAME,
					" deviceID = ? ", new String[] { String.valueOf(id) });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// closeDB();
		}
		return colunms;
	}

	@Override
	public int update(DeviceBean t, int id) {
		openWriteDB();
		try {
			database.update(DeviceBean.Table.T_NAME, beanToValues(t),
					" deviceID = ? ", new String[] { String.valueOf(id) });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// closeDB();
		}
		return 0;
	}

	@Override
	public ArrayList<DeviceBean> findAll() {
		openReadDB();
		ArrayList<DeviceBean> beans = new ArrayList<DeviceBean>();

		Cursor cursor = null;
		try {
			cursor = getCursor(null, null);
			while (cursor.moveToNext()) {
				beans.add(cursorToBean(cursor));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return beans;
	}

	public ArrayList<DeviceBean> findByTypeToObj(String type) {

		synchronized (LOCK) {
			openReadDB();
			ArrayList<DeviceBean> beans = new ArrayList<DeviceBean>();

			Cursor cursor = null;
			try {
				cursor = getCursor(" device_type = ? ", new String[] { type });
				while (cursor.moveToNext()) {
					beans.add(cursorToBean(cursor));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cursor != null) {
					cursor.close();
				}
				// closeDB();
			}
			return beans;
		}
	}

	@Override
	public ContentValues beanToValues(DeviceBean bean) {
		ContentValues values = new ContentValues();
		values.put(DeviceBean.Table.NAME, bean.device_name);
		values.put(DeviceBean.Table.TYPE, bean.device_type);
		return values;
	}

	@Override
	public DeviceBean cursorToBean(Cursor cursor) {
		DeviceBean bean = new DeviceBean();
		bean.device_name = cursor.getString(cursor.getColumnIndexOrThrow(DeviceBean.Table.NAME));
		bean.deviceID = cursor.getInt(cursor.getColumnIndexOrThrow(DeviceBean.Table.ID));
		bean.device_type = cursor.getString(cursor.getColumnIndexOrThrow(DeviceBean.Table.TYPE));
		bean.device_isSystem = cursor.getInt(cursor.getColumnIndexOrThrow(DeviceBean.Table.ISSYSTEM));
		return bean;
	}

	@Override
	public Cursor getCursor(String where, String[] args) {
		Cursor cursor = database.query(DeviceBean.Table.T_NAME, null, where,
				args, null, null, null);
		return cursor;
	}

	public ArrayList<String> findAllType() {
		synchronized (LOCK) {
			ArrayList<String> beans = new ArrayList<String>();
			openReadDB();
			Cursor cursor = null;
			try {
				cursor = database.query(true, DeviceBean.Table.T_NAME,
						new String[] { "device_type" }, null, null, null, null,
						null, null);
				while (cursor.moveToNext()) {
					beans.add(cursor.getString(0));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cursor != null) {
					cursor.close();
				}
			}
			return beans;
		}

	}

	public ArrayList<String> findAllName() {
		openReadDB();
		ArrayList<String> beans = new ArrayList<String>();

		Cursor cursor = null;
		try {
			cursor = getCursor(null, null);
			while (cursor.moveToNext()) {
				beans.add(cursorToBean(cursor).device_name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			// closeDB();
		}
		return beans;
	}

	public ArrayList<String> findByType(String content) {
		synchronized (LOCK) { 
			openReadDB();
			ArrayList<String> beans = new ArrayList<String>();

			Cursor cursor = null;
			try {
				cursor = getCursor(" device_type = ? ",new String[] { content });
				while (cursor.moveToNext()) {
					beans.add(cursorToBean(cursor).device_name);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cursor != null) {
					cursor.close();
				}
			}
			return beans;
		}
	}

	public String findTypeByName(String deviceName) {
		openWriteDB();
		String name = null;
		Cursor cursor = null;
		try {
			cursor = database.query(DeviceBean.Table.T_NAME,
					new String[] { "device_type" }, " device_name = ? ",
					new String[] { deviceName }, null, null, null);
			if (cursor != null) {
				cursor.moveToNext();
				name = cursor.getString(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			} 
		}

		return name;
	}
}
